package com.example.flower_shop.service.adm.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.entity.StoreProduct;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.StoreProductMapper;
import com.example.flower_shop.service.adm.StoreProductService;
import com.example.flower_shop.vo.product.ProductPageReq;
import com.example.flower_shop.vo.product.ProductReq;
import com.example.flower_shop.vo.product.ProductResp;
import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author zbxComputer
 * @description 针对表【sys_store_product(商品表)】的数据库操作Service实现
 * @createDate 2024-11-21 12:49:18
 */
@Service
public class StoreProductServiceImpl extends ServiceImpl<StoreProductMapper, StoreProduct>
        implements StoreProductService {

    @Resource
    private StoreProductMapper storeProductMapper;

    @Override
    public PageResult<StoreProduct> findAll(ProductPageReq req) {
        Page<StoreProduct> page = new Page<>(req.getPage(), req.getSize());
        LambdaQueryWrapper<StoreProduct> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(req.getKeyword())) {
            wrapper.like(StoreProduct::getStoreName, req.getKeyword());
        }
        wrapper.orderByDesc(StoreProduct::getCreateTime);
        Page<StoreProduct> iPage = storeProductMapper.selectPage(page, wrapper);
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords());
    }

    @Override
    public void delete(Long[] ids) {
        removeByIds(List.of(ids));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateProduct(ProductReq req) {
        LambdaQueryWrapper<StoreProduct> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StoreProduct::getId, req.getId());
        StoreProduct oldOne = getOne(wrapper);
        if (oldOne == null) {
            throw new exception("商品不存在");
        }
        StoreProduct product = BeanUtil.copyProperties(req, StoreProduct.class);
        if (req.getSliderImage() != null && req.getSliderImage().length > 0) {
            product.setSliderImage(transferImage(req.getSliderImage()));
        }
        try {
            if (!updateById(product)) {
                throw new exception("更新商品失败");
            }
        } catch (Exception e) {
            throw e;
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(ProductReq req) {
        StoreProduct product = BeanUtil.copyProperties(req, StoreProduct.class);
        if (req.getSliderImage() != null && req.getSliderImage().length > 0) {
            product.setSliderImage(transferImage(req.getSliderImage()));
        }
        if (!save(product)) {
            throw new exception("添加商品失败");
        }
    }

    @Override
    public ProductResp findById(Long id) {
        StoreProduct product = getById(id);
        if (product == null) {
            return new ProductResp();
        }
        return BeanUtil.copyProperties(product, ProductResp.class);
    }

    @Override
    public void changeStatusById(Long id) {
        StoreProduct product = getById(id);
        if (product == null) {
            throw new exception("商品不存在");
        }
        product.setIsShow(!product.getIsShow());
        if (!updateById(product)) {
            throw new exception("更新失败");
        }
    }

    private String transferImage(String[] images) {
        return JSONUtil.toJsonStr(images[1].contains("base64") ?
                List.of(images) : List.of(images[0] + "," + images[1]));
    }
}




